package com.wang.service.impl;

import com.wang.service.MailService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Objects;

/**
 * @Classname MailServiceImpl
 * @Description TODO
 * @Date 2024/5/29 9:31
 * @Created by pc
 */
@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private Configuration configuration;

    @Value("${spring.mail.from}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 收件人
        message.setTo(to);
        // 发件人
        message.setFrom(from);
        // 主题
        message.setSubject(subject);
        // 内容
        message.setText(content);
        javaMailSender.send(message);
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper ;
        try {
             messageHelper = new MimeMessageHelper(mimeMessage,true);
             messageHelper.setTo(to);
             messageHelper.setFrom(from);
             messageHelper.setSubject(subject);
             messageHelper.setText(content,true);
             javaMailSender.send(mimeMessage);
            System.out.println("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件异常");
        }
    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(content);
            // 带附件
            // 如果是从电脑中拿文件
            // FileSystemResource file = new FileSystemResource(new File(filePath));
            ClassPathResource resource = new ClassPathResource(filePath);
            // 将相对路径的文件转成绝对路径的文件
            FileSystemResource file = new FileSystemResource(resource.getFile());
            // 添加附件
            helper.addAttachment(Objects.requireNonNull(file.getFilename()),file);
            javaMailSender.send(message);
            System.out.println("发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("邮件异常");
        }

    }

    @Override
    public void sendModelMail(String to, String subject, String fileName, Object model) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            Template template = configuration.getTemplate(fileName);
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            helper.setText(html,true);
            javaMailSender.send(message);
            System.out.println("邮件已发送");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送异常");
        }
    }
}
